<template>
	<view>
		<xy-image class="cover" style="margin: 0 auto;width: 100%;background-color: #f1f1f1;" :src="info.cover" mode="aspectFit"></xy-image>
		<view class="item bg-white p-t p-l p-r p-b">
			<view class="title-box flex">
				<view class="title">
					{{info.title}}
				</view>
				<view class="price text-primary">
					{{info.price}}积分
				</view>
			</view>
			<view class="desc">
				{{info.description}}
			</view>
		</view>
		<!-- 地址 -->
		<navigator url="/pages/user_address/address/my?source=1" class="address-section">
			<view class="order-content">
				<text class="xyicon xyicon-address"></text>
				<view class="cen">
					<view class="top">
						<text class="name">{{addressData.name}}</text>
						<text class="mobile">{{addressData.tel}}</text>
					</view>
					<view class="address">
						{{addressData.city}}
						{{addressData.address}}
					</view>
				</view>
				<text class="xyicon xyicon-right"></text>
			</view>
			<image class="a-bg" src=""></image>
		</navigator>
		<view class="btn m-t m-l m-r">
			<button type="primary" @click="submit">我要兑换</button>
		</view>
		<view class="ps text-gray p-a">
			<view>
				1.兑换提交后不可取消，扣除积分不可恢复。
			</view>
			<view>
				2.兑换收货地址不可修改，请确认信息准确。
			</view>
			<view>
				3.兑换库存如售完请耐心等待。
			</view>
			<view>
				4.兑换活动最终解释权归{{$store.state.app.siteInfo.title}}所有。
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: 0,
				info: {},
				addressData: {
					name: '',
					tel: '',
					address: '',
					city: '',
					isDefault: 0,
				},
			};
		},
		onLoad(options) {
			this.id = options.id;
			this.loadData();
		},
		methods: {
			setAddress(addressData) {
				console.log(addressData);
				this.addressData = addressData;
			},
			async submit() {
				uni.showLoading({
					tilte: '加载中...'
				});
				let res = await this.util.request({
					url: '/v1/scoreshop/order/submit',
					method: 'post',
					data: {
						itemId: this.id,
						amount: 1,
						addressData: this.addressData
					}
				});
				if (res.code == 200) {
					uni.navigateTo({
						url: '../order/my',
					})
				} else {
					this.util.showToast({
						title: res.msg,
						icon: 'loading',
						duration: 2000
					});
				}
				uni.hideLoading();
			},
			async loadData() {
				uni.showLoading({
					tilte: '加载中...'
				});
				let res = await this.util.request({
					url: '/v1/scoreshop/item/info/' + this.id,
					method: 'get'
				});
				if (res.code == 200) {
					this.info = res.data.info;
					if (res.data.defaultAddress && res.data.defaultAddress.name) {
						this.addressData = res.data.defaultAddress;
					}
				} else {
					this.util.showToast({
						title: res.msg,
						icon: 'loading',
						duration: 2000
					});
				}
				uni.hideLoading();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.item {
		.title-box {
			justify-content: space-between;
		}
		.desc {
			margin-top: 10px;
			font-size: 14px;
			color: #777
		}
	}
	.ps {
		line-height: 22px;
		font-size: 13px;
	}
	.address-section {
		padding: 15px;
		background: #fff;
		position: relative;
		.order-content {
			display: flex;
			align-items: center;
		}
		.icon-shouhuodizhi {
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 90upx;
			color: #888;
			font-size: 44upx;
		}
		.cen {
			padding-left: 5px;
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28upx;
			color: $font-color-dark;
		}
		.name {
			font-size: 34upx;
			margin-right: 24upx;
		}
	
		.address {
			margin-top: 16upx;
			margin-right: 20upx;
			color: $font-color-light;
		}
	
		.icon-you {
			font-size: 32upx;
			color: $font-color-light;
			margin-right: 30upx;
		}
	
		.a-bg {
			position: absolute;
			left: 0;
			bottom: 0;
			display: block;
			width: 100%;
			height: 5upx;
		}
	}
</style>
